using System;
using System.Data;
using System.Data.SQLite;
using System.Windows.Forms;
namespace DataBaseTool
{
    class SuperDAO
    {
        public SuperDAO()
        {
        }
        public SQLiteConnection getConnection()
        {
            string server = "D:/c_sharp_git/dawayiSY/Dawayi/Dawayi.db";
            string database = "Dawayi";
            string password = "MMeshelR";
            string connectionStringTest = @"Data Source= " + server + "; database = " + database + "; Version=3; password = " + password + ";  ";
            return new SQLiteConnection(connectionStringTest);
        }
        public DataTable getData(string tableName, string where)
        {
            string sql = "select* from " + tableName;
            if (!string.IsNullOrEmpty(where))
            {
                sql += " where " + where;
            }
            DataTable dataTable = Select(sql);//select * from T where id=?1
            return dataTable;
        }
        public DataTable Select(string sql,params object[] args)
        {
            SQLiteConnection conn = getConnection();
            SQLiteCommand cmd = null;
            
             DataTable dt = new DataTable();
            try
            {
                conn.Open();
                cmd = new SQLiteCommand(sql, conn);
                SQLiteDataAdapter sda = new SQLiteDataAdapter(cmd);
                sda.Fill(dt);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            try
            {
                conn.Close();
                cmd.Dispose();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            return dt;
        }
        public void ExecuteSQLTran(string sqlString)
        {
            SQLiteConnection con = getConnection();
            SQLiteCommand cmd = new SQLiteCommand(sqlString, con);
            con.Open();
            SQLiteTransaction tran = con.BeginTransaction();
            cmd.Transaction = tran; try
            {
                cmd.ExecuteNonQuery();
                tran.Commit();
            }
            catch (Exception e)
            {
                tran.Rollback();
            }
            finally
            {
                con.Close();
            }
        }

    }
}